library(readr)
library(ggplot2)
library(dplyr)
library(methods)
library(stringi)
library(keras)
resnet50 <- application_resnet50(weights = 'imagenet', include_top = TRUE)
model_embed <- keras_model(inputs = resnet50$input,
outputs = get_layer(resnet50, 'avg_pool')$output)
reading in the files
flowers <- read_csv("my-image-data.csv")
## Parsed with column specification:
## cols(
## obs_id = col_character(),
## train_id = col_character(),
## class = col_integer(),
## class_name = col_integer(),
## path_to_image = col_character()
## )
xx4 <- read_rds("flowers_embed.rds")
creating X and Y matrices
X <- t(apply(xx4, 1, cbind))
y <- flowers$class
We print one sample image1 each of 20 different classes
image_path <- "oxford-102-flowers/jpg/image_00288.jpg"
image <- image_load(image_path, target_size = c(224,224))
image <- image_to_array(image)
image <- array_reshape(image, c(1, dim(image)))
dim(image)
## [1] 1 224 224 3
par(mar = rep(0, 4L))
plot(0,0,xlim=c(0,1),ylim=c(0,1),axes= FALSE, type = "n", asp=1)
rasterImage(image[1,,,] / 255,0,0,1,1)
image_path <- "oxford-102-flowers/jpg/image_00920.jpg"
image <- image_load(image_path, target_size = c(224,224))
image <- image_to_array(image)
image <- array_reshape(image, c(1, dim(image)))
dim(image)
## [1] 1 224 224 3
par(mar = rep(0, 4L))
plot(0,0,xlim=c(0,1),ylim=c(0,1),axes= FALSE, type = "n", asp=1)
rasterImage(image[1,,,] / 255,0,0,1,1)
now this model taking the images and making a prediction model out of it
X_train <- X[flowers$train_id == "train",]
y_train <- to_categorical(flowers$class[flowers$train_id == "train"])
model <- keras_model_sequential()
model %>%
layer_dense(units = 256, input_shape = ncol(X_train)) %>%
layer_activation(activation = "relu") %>%
layer_dropout(rate = 0.5) %>%
layer_dense(units = 256) %>%
layer_activation(activation = "relu") %>%
layer_dropout(rate = 0.5) %>%
layer_dense(units = ncol(y_train)) %>%
layer_activation(activation = "softmax")
model %>% compile(loss = 'categorical_crossentropy',
optimizer = optimizer_rmsprop(lr = 0.001 / 2),
metrics = c('accuracy'))
history <- model %>% fit(X_train, y_train, epochs = 20)
plot(history)
y_pred <- predict_classes(model, X)
tapply(y == y_pred, flowers$train_id, mean)
## train valid
## 0.9953224 0.9205379
which.max takes the best prediction from each class and draws them
set.seed(1)
par(mfrow = c(2, 3))
y_pred_mat <- predict(model, X)
id <- apply(y_pred_mat, 2, which.max)
for (i in id) try({
par(mar = rep(0, 4L))
plot(0,0,xlim=c(0,1),ylim=c(0,1),axes= FALSE,type = "n")
Z <- image_to_array(image_load(flowers$path_to_image[i], target_size = c(224,224)))
rasterImage(Z /255,0,0,1,1)
text(0.5, 0.1, label = flowers$class_name[y_pred[i] + 1L], col = "blue", cex=2)
})